package com.android.contacts.service;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentResolver;

import com.android.contacts.ContactsApiSdk5;
import com.android.contacts.ContactsApiSdk5.ContactsBackCallback;
import com.android.contacts.model.vo.Contact;
import com.android.contacts.model.vo.VOAssembler;

public class ServiceDelegate {

	private final String TAG = ServiceDelegate.class.getSimpleName();
	private static final ServiceDelegate INSTANCE = new ServiceDelegate();
	private ServiceActivator mServiceLocator = new ServiceActivator();
	private VOAssembler mVoAssembler = new VOAssembler();

	private ServiceDelegate() {
	}

	public static ServiceDelegate getInstance() {
		return INSTANCE;
	}

	// TODO : do some changes for restore.
	public ArrayList<Contact> getContactList(boolean isLocal, String url,
			ContentResolver contentResolver,
			ContactsBackCallback contactsBackCallback) {
		ContactsApiSdk5 contactsApiSdk5 = new ContactsApiSdk5();
		contactsApiSdk5.setContentResolver(contentResolver);
		contactsApiSdk5.setContactsBackCallback(contactsBackCallback);
		ArrayList<Contact> contactList = null;
		if (isLocal) {
			contactList = contactsApiSdk5.getContactList().getContacs();
			return contactList;
		} else {
			String dataUrl = ServiceLocator.getInstance().getURL(url);
			InputStream is = mServiceLocator.invoke(dataUrl);
			return mVoAssembler.getContactList(is);
		}

	}
}
